import { nanoid } from 'nanoid';
import axios from 'axios';

export default {
  namespaced: true,
  actions: {
    addWang(context, value) {
      if (value.indexOf('王') == 0) {
        const info = {
          id: nanoid(),
          name: value,
        }
        context.commit('saveUser', info)
      } else {
        alert('必须姓王才能添加')
      }
    },
    addService(context) {
      axios.get('http://api.uixsj.cn/hitokoto/get?type=social').then(response => {
        const info = {
          id: nanoid(),
          name: response.data,
        }
        context.commit('saveUser', info)
      }).catch(error => {
        alert(error.message)
      })
    }
  },
  mutations: {
    saveUser(state, value) {
      state.userList.unshift(value)
    }
  },
  state: {
    userList: []
  },
  getters: {
    getFirstName(state) {
      return state.userList[0] && state.userList[0].name
    }
  }
}